//
//  MainViewController.swift
//  sina----
//
//  Created by mac on 16/5/8.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
     
        setTabbar()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setUpcomposeButton()
    }
    lazy var composeButton: UIButton = UIButton(
        title:nil,
        imageName: "tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")
}
// MARK: - 设置tabbar
extension MainViewController {
    
    private func setTabbar() {
        //两句代码必须一起使用, 同样也可以处理Nav导航栏
        tabBar.shadowImage = UIImage.mc_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        //添加compose按钮
        tabBar.addSubview(composeButton)
    }

    func setUpcomposeButton() {
    
        tabBar.bringSubviewToFront(composeButton)
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        
    }

}

// MARK: - 添加子控制器
extension MainViewController {

    private func addChildViewControllers() {
        
        addChildViewController(HoomViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MessageViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(ProfileViewController(), imageName: "tabbar_profile", title: "个人")
    }
    
    private func addChildViewController(vc: UIViewController, imageName: String, title: String) {
        
//        let vc: UIViewController = HoomViewController()
        vc.title = title
        //设置tabbar文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        //设置tabbar文字大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(12)], forState: .Normal)
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
}